var totalPage;
var totalCnt;
var chargingStationLogs= [];
var tabFlag = "chargingStationLog";

$(document).ready(function() {
    $('title', parent.document).html($('title').text());
    selectDate("startDate", 0);
	selectDate("endDate", 0);
	setBaseUrl();
//	document.getElementById("total").innerText = '共0页  / 共0条记录';
	$(".turnPage").createTurnPage({dispalyInputPage: false, backFn: search });
	$ajaxList('chargingStation', getChargingStation);
	search();
});
function getChargingStation(data) {
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#ChargingStationID").append("<option value='" + data[i].chargingStationID + "'>" + data[i].chargingStationName + "</option>");
	}
}
function allClick(flag){
	tabFlag = flag;
	var logContent = $("#logContent");
	if (logContent && logContent.html()) {
		logContent.html("");
	}
	var table = undefined;
	var rows = 0;
	
	$('#currentPage').val(1);
	
	if(tabFlag == "chargingStationLog"){
		
	} else if (tabFlag == "checkRecord") {
		table = document.getElementById('checkRecordThead');
		rows = table.rows && table.rows.length;
		if (rows == 0) {
			createTableHead("checkRecordThead", checkRecordTable, searchCheckRecord, search);
		}
	} else if (tabFlag == "onDutyLog") {
		table = document.getElementById('onDutyLogThead');
		rows = table.rows && table.rows.length;
		if (rows == 0) {
			createTableHead("onDutyLogThead", onDutyLogTable, searchOnDutyLog, search);
		}
	} else if (tabFlag == "replacePart") {
		table = document.getElementById('replacePartThead');
		rows = table.rows && table.rows.length;
		if (rows == 0) {
			createTableHead("replacePartThead", replacePartTable, searchReplacePart, search);
		}
	} else if (tabFlag == "breakRepair" && $('#breakRepairThead').length > 0) {
		table = document.getElementById('breakRepairThead');
		rows = table.rows && table.rows.length;
		if (rows == 0) {
			createTableHead("breakRepairThead", breakRepairTable, searchBreakRepair, search);
		}
	}
	search();
}
function resetSearch(){
	setTimeout(search, 500);
}
function search(){
	showLoading();
	if (tabFlag == "chargingStationLog") {
		$ajaxCommon('searchChargingStationLog', searchChargingStationLog, $('#searchChargingStationLog').serialize());
	} else if (tabFlag == "checkRecord") {
		$ajax('searchCheckRecord', 'searchChargingStationLog', searchCheckRecord);
	} else if (tabFlag == "onDutyLog") {
		$ajax('searchOnDutyLog', 'searchChargingStationLog', searchOnDutyLog);
	} else if (tabFlag == "replacePart") {
		$ajax('searchReplacePartRecord', 'searchChargingStationLog', searchReplacePart);
	} else if (tabFlag == "breakRepair") {
		$ajax('searchBreakRepairRecord', 'searchChargingStationLog', searchBreakRepair);
	}
}

//机柜日志查询
function searchChargingStationLog(data) {
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
//	document.getElementById("total").innerText = '共' + totalPage + '页 / 共' + totalCnt + '条记录 ';
	searchLogData = data && data.log || [];
	$(".turnPage").resetTurnPage();
	var logContent = $("#logContent");
	if (logContent && logContent.html()) {
		logContent.html("");
	}
	var currentPage = parseInt($('#currentPage').val(), 10) || 1;
	var rows = parseInt($('#rows').val(), 10);
	var logContent = $("#logContent");
    var dateLength = searchLogData.length;
    if (dateLength == 0) {
		showMessge(messageContent["Search.NoResultFound"]);
		$('#currentPage').val(1);
		$("#page").val(1);
	} else {
		for(var j=0; j<dateLength; j++){
			logContent.append('<div class="form-group" style="margin-top:20px"><div class="col-sm-10 partCondition">' + timeStamp2String(searchLogData[j].timestamp) + '</div></div>');
			logContent.append('<div class="partTable partTableLog"><p style="color: #666;float:left">' + searchLogData[j].message + '</p></div>');
		}
	}
    setTimeout(hideLoading, 500);
}
//检查记录查询
function searchCheckRecord(data) {
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
	checkRecordData = data && data.checkRecordList || [];
	createTableBody("checkRecordTbody", checkRecordTable, checkRecordData);
}
//值班日志查询
function searchOnDutyLog(data) {
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
	onDutyLogData = data && data.dutyLogList || [];
	createTableBody("onDutyLogTbody", onDutyLogTable, onDutyLogData);
}
//零件更换记录查询
function searchReplacePart(data) {
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
	changePartData = data && data.changePartList || [];
	createTableBody("replacePartTbody", replacePartTable, changePartData);
}
//故障维修记录查询
function searchBreakRepair(data) {
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
	repairFauleHistoriesDate = data && data.repairFauleHistoriesList || [];
	createTableBody("breakRepairTbody", breakRepairTable, repairFauleHistoriesDate);
}

function details(index){
	allTable(index);
}
function allTable(index) {
	if (tabFlag == "checkRecord") {
		createDetailHead("detailThead", checkRecordTable, 1);
		createDetailBody("detailTbody", checkRecordTable, checkRecordData[index], 1);
		createDetailAll("detailTable", checkRecordTable, checkRecordData[index], 2);
		setFormDisabled("saveForm");
	} else if (tabFlag == "onDutyLog") {
		createDetailHead("detailThead", onDutyLogTable, 1);
		createDetailBody("detailTbody", onDutyLogTable, onDutyLogData[index], 1);
		createDetailAll("detailTable", onDutyLogTable, onDutyLogData[index], 2);
		setFormDisabled("saveForm");
	} else if (tabFlag == "replacePart") {
		createDetailHead("detailThead", replacePartTable, 1);
		createDetailBody("detailTbody", replacePartTable, changePartData[index], 1);
		createDetailAll("detailTable", replacePartTable, changePartData[index], 2);
		setFormDisabled("saveForm");
	} else if (tabFlag == "breakRepair") {
		createDetailHead("detailThead", breakRepairTable, 1);
		createDetailBody("detailTbody", breakRepairTable, repairFauleHistoriesDate[index], 1);
		createDetailAll("detailTable", breakRepairTable, repairFauleHistoriesDate[index], 2);
		setFormDisabled("saveForm");
	}
    $('#save').modal('show');
}
